package com.project.hqshorttaxi.controller;

import com.github.pagehelper.PageHelper;
import com.project.hqshorttaxi.aop.TagLog;
import com.project.hqshorttaxi.dto.RoleDto;
import com.project.hqshorttaxi.dto.RoleMenuDto;
import com.project.hqshorttaxi.dto.RoleMenuParam;
import com.project.hqshorttaxi.dto.RoleParam;
import com.project.hqshorttaxi.entity.Login;
import com.project.hqshorttaxi.entity.Role;
import com.project.hqshorttaxi.entity.RoleMenu;
import com.project.hqshorttaxi.service.LoginService;
import com.project.hqshorttaxi.service.RoleMenuService;
import com.project.hqshorttaxi.service.RoleService;
import com.project.hqshorttaxi.util.PageInfo;
import com.project.hqshorttaxi.util.RestResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/role/")
public class RoleController {

    @Resource
    RoleService roleService;
    @Resource
    RoleMenuService roleMenuService;
    @Resource
    private LoginService loginService;

    /**
     * 新增限权
     */
    @PostMapping("add")
    @TagLog(operation = 2, content = "新增限权")
    public RestResult add(@RequestBody Role param) {
        try {
            List<Role> list = roleService.listAll();
            param.setRoleId(list.size() + 1);
            roleService.insert(param);
            return RestResult.ok();
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
        }
    }

    /**
     * 更新限权
     */
    @PostMapping("update")
    @TagLog(operation = 3, content = "更新限权")
    public RestResult update(@RequestBody Role param) {
        try {
            Role entity = roleService.getById(param.getId());
            if (entity == null) {
                return RestResult.error("限权不存在");
            } else {
                param.setUpdateTime(new Date());
                roleService.update(param);
                return RestResult.ok();
            }
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
        }
    }

    /**
     * 删除限权
     */
    @PostMapping("delete/{id}")
    @TagLog(operation = 4, content = "删除限权")
    public RestResult userDelete(@PathVariable Integer id) {
        try {
            Role entity = roleService.getById(id);
            if (entity == null) {
                return RestResult.error("限权不存在");
            } else {
                List<Login> userList = loginService.getByRoleId(entity.getRoleId());
                if (userList.size() > 0) {
                    return RestResult.error("该限权已存在使用用户");
                }
                entity.setDeleteFlag(1);
                entity.setUpdateTime(new Date());
                roleService.update(entity);
                List<RoleMenu> menuRoles = roleMenuService.getByRoleId(entity.getRoleId());
                if (menuRoles.size() > 0) {
                    for (RoleMenu menuRole : menuRoles) {
                        menuRole.setDeleteFlag(1);
                        menuRole.setUpdateTime(new Date());
                        roleMenuService.update(menuRole);
                    }
                }
                return RestResult.ok();
            }
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
        }
    }

    /**
     * 限权列表
     */
    @PostMapping("page")
    @TagLog(operation = 0, content = "查询限权列表")
    public RestResult page(@RequestBody RoleParam param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        return RestResult.ok(new PageInfo<>(roleService.list(param)));
    }

    /**
     * 根据ID获取限权
     */
    @GetMapping("{id}")
    @TagLog(operation = 0, content = "根据ID获取限权详细信息")
    public RestResult pageById(@PathVariable Integer id) {
        return RestResult.ok(roleService.getById(id));
    }

    /**
     * 限权所有菜单列表
     */
    @PostMapping("menu")
    @TagLog(operation = 0, content = "查询限权所有菜单列表")
    public RestResult menu(@RequestHeader(value = "authorization") String token) {
        Login entity = loginService.getByToken(token);
        if (entity == null) {
            return RestResult.error("用户不存在");
        }
        Role role = roleService.getByRoleId(entity.getRoleId());
        RoleDto roleDto = new RoleDto();
        roleDto.setRole(role);
        roleDto.setUserId(entity.getUserId());
        roleDto.setUserName(entity.getUserName());
        RoleMenuParam param = new RoleMenuParam();
        param.setRoleId(entity.getRoleId());
        List<RoleMenuDto> menuList = roleMenuService.getMenuByRoleId(param);
        roleDto.setMenuList(menuList);
        return RestResult.ok(roleDto);
    }

    /**
     * 根据限权ID获取所有菜单列表
     */
    @PostMapping("menu/byRoleId/{id}")
    @TagLog(operation = 0, content = "根据限权ID获取所有菜单列表")
    public RestResult menuByRoleId(@PathVariable Integer id) {
        Role entity = roleService.getById(id);
        if (entity == null) {
            return RestResult.error("限权不存在");
        }
        return RestResult.ok(roleMenuService.getByRoleId(id));
    }

    /**
     * 更新限权所有菜单列表
     */
    @PostMapping("menu/update")
    @TagLog(operation = 3, content = "更新限权所有菜单列表")
    public RestResult menuUpdate(@RequestBody RoleMenuParam param) {
        try {
            List<RoleMenu> roleMenus = roleMenuService.getByRoleId(param.getRoleId());
            if (roleMenus.size() > 0) {
                for (RoleMenu menuRole : roleMenus) {
                    menuRole.setDeleteFlag(1);
                    menuRole.setUpdateTime(new Date());
                    roleMenuService.update(menuRole);
                }
            }
            for (int i = 0; i < param.getMenuIds().length; i++) {
                RoleMenu menuRole = new RoleMenu();
                menuRole.setRoleId(param.getRoleId());
                menuRole.setMenuId(Integer.parseInt(param.getMenuIds()[i]));
                menuRole.setType(0);
                for (RoleMenu roleMenu : param.getRoleMenus()) {
                    if (roleMenu.getMenuId() == menuRole.getMenuId()) {
                        menuRole.setType(roleMenu.getType());
                    }
                }
                roleMenuService.insert(menuRole);
            }
            return RestResult.ok();
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
        }
    }
}
